//
//  LGSelectNormController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/2.
//  选择指标

#import "LGSelectNormController.h"
#import "LGAnomalyNormCell.h"
@interface LGSelectNormController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *bottomView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGSelectNormController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"选择指标";
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self textFieldEditingChanged:self.searchTF];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    [RequestUtil withPOST:@"/doctor/other.inspection_report_type/itemListByTypeId" parameters:@{@"type_id":self.normid,@"keyword":textField.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGAnomalyNormCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    if(self.selectNormData){
        self.selectNormData(tempDic);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(10);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(10))];
    headView.backgroundColor = [UIColor clearColor];
    headView.topMargin = viewPix(5);
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(20);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(20))];
    footView.backgroundColor = [UIColor clearColor];
    footView.bottomMargin = viewPix(15);
    return footView;
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        [_searchTF setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _searchTF.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.placeholder = @"请输入关键字或缩写搜索";
        _searchTF.font = LGFont(16);
        _searchTF.leftView = [self leftView];
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.clearButtonMode =  UITextFieldViewModeWhileEditing;
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTF;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(80), Screen_W, Screen_H-topBarHeight-viewPix(80)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGAnomalyNormCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
    }
    return _tableView;
}

-(UIView *)leftView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(35), viewPix(54))];
    UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchGray16"]];
    iconView.frame = CGRectMake(viewPix(12), viewPix(19), viewPix(16), viewPix(16));
    [baseView addSubview:iconView];
    return baseView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
